package logic

import (
	"context"
	"fmt"
	"src/models/mysql"
	"src/models/redis"

	"src/internal/svc"
	"src/src"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *src.LoginRequest) (*src.LoginResponse, error) {
	// todo: add your logic here and delete this line

	get := redis.Client.Get("SendSms" + in.Tel + in.Source)

	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信未验证")
	}
	var login mysql.Account
	err := mysql.DB.Debug().Where("tel = ?", in.Tel).Find(&login).Error
	if err != nil {
		return nil, err
	}

	return &src.LoginResponse{Id: int64(login.ID)}, nil
}
